<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>geo_point(地理坐标点)数据类型 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'geo-point.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/geo-point.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/geo-point.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/geo-point.html" rel="nofollow" target="_blank">../en/geo-point.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">映射</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">字段数据类型</a></span>
»
<span class="breadcrumb-node">geo_point(地理坐标点)数据类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="flattened.html">« flattened(扁平)数据类型</a>
</span>
<span class="next">
<a href="geo-shape.html">geo_shape(地理形状)数据类型 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geo-point"></a>geo_point(地理坐标点)数据类型
</h2>
</div></div></div>

<p><code class="literal">geo_point</code>类型的字段接受经度-纬度对，可用于：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
查找<a class="xref" href="query-dsl-geo-bounding-box-query.html" title="geo-bounding box查询">边界框(bounding box)</a>内、中心点的特定<a class="xref" href="query-dsl-geo-distance-query.html" title="geo-distance查询">距离(distance)</a>内、<a class="xref" href="query-dsl-geo-polygon-query.html" title="geo-polygon查询">多边形(polygon)</a>内或<a class="xref" href="query-dsl-geo-shape-query.html" title="geo-shape查询">geo_shape查询</a>内的地理坐标点。
</li>
<li class="listitem">
按<a class="xref" href="search-aggregations-bucket-geohashgrid-aggregation.html" title="geohash网格聚合">地理位置</a>或离中心点的<a class="xref" href="search-aggregations-bucket-geodistance-aggregation.html" title="geo距离聚合">距离(distance)</a>来聚合文件。
</li>
<li class="listitem">
将距离整合到文档的<a class="xref" href="query-dsl-function-score-query.html" title="函数评分查询">相关性评分</a>中。
</li>
<li class="listitem">
按距离<a class="xref" href="search-request-body.html#geo-sorting" title="Geo Distance Sorting">排序(sort)</a>文档。
</li>
</ul>
</div>
<p>有五种方法可以指定 geo-point，如下所示：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT my_index/_doc/1
{
  "text": "Geo-point as an object",
  "location": { <a id="CO296-1"></a><i class="conum" data-value="1"></i>
    "lat": 41.12,
    "lon": -71.34
  }
}

PUT my_index/_doc/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" <a id="CO296-2"></a><i class="conum" data-value="2"></i>
}

PUT my_index/_doc/3
{
  "text": "Geo-point as a geohash",
  "location": "drm3btev3e86" <a id="CO296-3"></a><i class="conum" data-value="3"></i>
}

PUT my_index/_doc/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] <a id="CO296-4"></a><i class="conum" data-value="4"></i>
}

PUT my_index/_doc/5
{
  "text": "Geo-point as a WKT POINT primitive",
  "location" : "POINT (-71.34 41.12)" <a id="CO296-5"></a><i class="conum" data-value="5"></i>
}

GET my_index/_search
{
  "query": {
    "geo_bounding_box": { <a id="CO296-6"></a><i class="conum" data-value="6"></i>
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/632.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>表示为对象的geo-point，带有<code class="literal">lat</code>(纬度)和<code class="literal">lon</code>(经度)键。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>以字符串形式表示的geo-point，格式为：<code class="literal">"lat,lon"</code>。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>表示为 geohash 的geo-point。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>以数组形式表示的geo-point，格式为：[ <code class="literal">lon</code>, <code class="literal">lat</code>]。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>表示为<a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html" class="ulink" target="_top">众所周知的文本</a>点，格式为：<code class="literal">"POINT(lon lat)"</code></p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO296-6"><i class="conum" data-value="6"></i></a></p>
</td>
<td align="left" valign="top">
<p>
一种地理边界框查询，用于查找落在该框内的所有 geo-point。
</p>
</td>
</tr>
</table>
</div>
<div class="important admon">
<div class="icon"></div>
<div class="admon_content">
<h3>表示为数组或字符串的 geo-point</h3>
<p>请注意，字符串格式的 geo-point 按 <code class="literal">lat,lon</code>排序，而数组格式的 geo-point 按相反顺序排序：<code class="literal">lon,lat</code>。</p>
<p>最初，<code class="literal">lat,lon</code>顺序都在数组和字符串中使用，但是数组格式在早期被改变以符合 GeoJSON 使用的格式。</p>
</div>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>
一个地理位置坐标点可以表示为一个<a href="http://en.wikipedia.org/wiki/Geohash" class="ulink" target="_top">geohash</a>值。
<br/>
<br/>
<br/>
geohash 是经纬度交织的<a href="https://en.wikipedia.org/wiki/Base32" class="ulink" target="_top">base32</a>编码的字符串。

geohash 中的每个字符都会为精度增加5比特位。

所以 hash 越长，就越精确。

出于索引的目的，geohash被转换成纬度-经度对。

在此过程中，仅使用前12个字符，因此在 geohash 中指定超过12个字符并不会提高精度。

12个字符提供了60个比特位，这应该会将可能的误差减少到小于2厘米。
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="geo-point-params"></a><code class="literal">geo_point</code>字段的参数
</h3>
</div></div></div>
<p><code class="literal">geo_point</code>字段接受以下参数：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<a class="xref" href="ignore-malformed.html" title="ignore_malformed"><code class="literal">ignore_malformed</code></a>
</p>
</td>
<td valign="top">
<p>
如果为<code class="literal">true</code>，将忽略格式错误的geo-point值。

如果为<code class="literal">false</code>(默认)，格式错误的geo-point值会引发异常并拒绝整个文档。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">ignore_z_value</code>
</p>
</td>
<td valign="top">
<p>
如果为<code class="literal">true</code>(默认)，将接受三个维度点(存储在 source 中),但仅索引纬度和经度值；第三维度被忽略。

如果为<code class="literal">false</code>，包含任何超过纬度和经度(二维)值的 geo-point 值会引发异常并拒绝整个文档。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="null-value.html" title="null_value"><code class="literal">null_value</code></a>
</p>
</td>
<td valign="top">
<p>
接受 geo-point 值，该值将替代任何显式的<code class="literal">null</code>值。

默认为<code class="literal">null</code>，这意味着该字段被视为缺失。
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_using_geo_points_in_scripts"></a>在脚本中使用 geo-point
</h3>
</div></div></div>
<p>
当在脚本中访问 geo-point 的值时，该值作为一个<code class="literal">GeoPoint</code>对象返回，该对象允许分别访问<code class="literal">.lat</code>和<code class="literal">.lon</code>值：
</p>
<div class="pre_wrapper lang-painless">
<pre class="programlisting prettyprint lang-painless">def geopoint = doc['location'].value;
def lat      = geopoint.lat;
def lon      = geopoint.lon;</pre>
</div>
<p>出于性能原因，最好直接访问 lat/lon 的值：</p>
<div class="pre_wrapper lang-painless">
<pre class="programlisting prettyprint lang-painless">def lat      = doc['location'].lat;
def lon      = doc['location'].lon;</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="flattened.html">« flattened(扁平)数据类型</a>
</span>
<span class="next">
<a href="geo-shape.html">geo_shape(地理形状)数据类型 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>